/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import shared.Card;

/**
 *
 * @author gekon
 */
public class CardsCachePanel extends JPanel
{

    private ArrayList<Card> finalCards = new ArrayList<Card>();
    private ArrayList<Point> finalCardsPositions = new ArrayList<Point>();
    private ArrayList<Card> canceledCards = new ArrayList<Card>();
    private int selectedCard = -1;
    private Boolean newCall = true;
    private JScrollPane scrollPane;
    private JSlider positionSlider;

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if (selectedCard != -1)
        {
            g.drawImage(new ImageIcon("src/images/extra/selectCardLight.png").getImage(), finalCardsPositions.get(selectedCard).x + 5, 0, this);
        }

        for (Card card : finalCards)
        {
            if (card.getAsJoker() == null)
            {
                g.drawImage(new ImageIcon("src/images/" + getImagePackage(card.getSuit()) + "/" + getImageFileName(card.getType()) + ".png").getImage(),
                        finalCardsPositions.get(finalCards.indexOf(card)).x, finalCardsPositions.get(finalCards.indexOf(card)).y, this);
            } else
            {
                g.drawImage(new ImageIcon("src/images/" + getImagePackage(card.getAsJoker().getSuit()) + "/" + getImageFileName(card.getAsJoker().getType()) + ".png").getImage(),
                        finalCardsPositions.get(finalCards.indexOf(card)).x, finalCardsPositions.get(finalCards.indexOf(card)).y, this);
                g.drawImage(new ImageIcon("src/images/extra/starIcon.png").getImage(), finalCardsPositions.get(finalCards.indexOf(card)).x + 60, finalCardsPositions.get(finalCards.indexOf(card)).y + 100, this);
            }
        }

        this.setPreferredSize(new Dimension(finalCards.size() * 92, 150));
        this.revalidate();

    }

    public void setElements(JScrollPane scrollPane, JSlider positionSlider)
    {
        this.positionSlider = positionSlider;
        this.scrollPane = scrollPane;
    }

    public ArrayList<Card> getFinalCards()
    {
        ArrayList<Card> result  = new ArrayList<Card>();
        
        for( Card c : finalCards )
        {
            if( c.getAsJoker() != null )
            {
                result.add(new Card(c.getSuit(), c.getType(), new Card(c.getAsJoker().getSuit(), c.getAsJoker().getType())));
            }
            else
            {
                result.add(new Card(c.getSuit(), c.getType()));
            }
        }
        
        return result;
    }

    public void addCard(Card card)
    {
        if (newCall == true)
        {
            selectedCard = -1;
            finalCards.clear();
            finalCardsPositions.clear();
            canceledCards.clear();
            newCall = false;
        } else
        {
            positionSlider.setMaximum(positionSlider.getMaximum() + 1);
        }

        finalCardsPositions.add(new Point(92 * finalCards.size(), 5));
        finalCards.add(card);
        this.repaint();
    }

    public void selectCard(int x, int y)
    {
        for (int i = 0; i < finalCards.size(); i++)
        {
            if ((x > finalCardsPositions.get(i).x) && (x < (finalCardsPositions.get(i).x + 92)) && (y < 135) && (y > 5))
            {
                if (finalCards.size() > 1)
                {
                    selectedCard = i;
                    positionSlider.setEnabled(true);
                    positionSlider.setValue(selectedCard);
                }
                this.repaint();
                break;
            }
        }
    }

    public ArrayList<Card> cancelSelect()
    {
        if (selectedCard != -1)
        {
            canceledCards.clear();
            canceledCards.add(new Card(finalCards.get(selectedCard).getSuit(), finalCards.get(selectedCard).getType()));
            finalCards.remove(selectedCard);
            finalCardsPositions.remove(selectedCard);

            for (int i = selectedCard; i < finalCardsPositions.size(); i++)
            {
                finalCardsPositions.get(i).move(i * 92, 5);
            }

            positionSlider.setMaximum(positionSlider.getMaximum() - 1);
            selectedCard = -1;
            positionSlider.setEnabled(false);
            this.repaint();
            return canceledCards;
        } else
        {
            return null;
        }
    }

    public ArrayList<Card> cancelAll()
    {
        canceledCards.clear();

        for (Card card : finalCards)
        {
            canceledCards.add(new Card(card.getSuit(), card.getType()));
        }

        positionSlider.setMaximum(0);
        positionSlider.setEnabled(false);
        finalCards.clear();
        finalCardsPositions.clear();
        selectedCard = -1;
        this.repaint();
        return canceledCards;
    }

    public void changeOrder(int value)
    {
        int suit = -1;
        int type = -1;
        int suitAsJocker = -1;
        int typeAsJocker = -1;

        if (selectedCard != -1)
        {
            suit = finalCards.get(value).getSuit();
            type = finalCards.get(value).getType();
            if (finalCards.get(value).getAsJoker() != null)
            {
                suitAsJocker = finalCards.get(value).getAsJoker().getSuit();
                typeAsJocker = finalCards.get(value).getAsJoker().getType();
                finalCards.remove(value);
                finalCards.add(selectedCard, new Card(suit, type, new Card(suitAsJocker, typeAsJocker)));
            } else
            {
                finalCards.remove(value);
                finalCards.add(selectedCard, new Card(suit, type));
            }

            selectedCard = value;
            this.repaint();
        }
    }

    public void setNewCall(Boolean newCall)
    {
        this.newCall = newCall;
        selectedCard = -1;
        finalCards = new ArrayList<Card>();
        finalCardsPositions = new ArrayList<Point>();
        canceledCards = new ArrayList<Card>();
        positionSlider.setMinimum(0);
        positionSlider.setMaximum(0);
        this.repaint();
    }

    private String getImagePackage(int cardSuit)
    {
        switch (cardSuit)
        {
            case Card.NONE_SUIT:
                return "extra";
            case Card.HEART:
                return "heart";
            case Card.DIAMOND:
                return "diamond";
            case Card.CLUB:
                return "club";
            case Card.SPADE:
                return "spade";
        }
        return null;
    }

    private String getImageFileName(int cardType)
    {
        if (cardType <= Card.TEN)
        {
            return Integer.toString(cardType);
        } else
        {
            switch (cardType)
            {
                case Card.ACE:
                    return "A";
                case Card.KING:
                    return "K";
                case Card.QUEEN:
                    return "Q";
                case Card.JACK:
                    return "J";
                case Card.JOKER:
                    return "jocker";
            }
        }
        return null;
    }
}
